using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Engine;
using Engine.Graphics;
using Engine.Input;
using Game;

namespace RuthlessConquest {
    public class MainMenuScreen : Screen {
        //public string VersionString = string.Empty;
        public RectangleWidget LogoWidget;

        //public MotdWidget MotdWidget;
        public ButtonWidget PracticeButton;
        public ButtonWidget PlayButton;
        public ButtonWidget SettingsButton;
        public ButtonWidget MoreGamesButton;
        public ButtonWidget SwitchLanguageButton;
        public ButtonWidget SCEntryButton;
        public const string fName = "RCMainMenuScreen";

        public MainMenuScreen() {
            Widget[] root = [new BackgroundWidget(), new BoidsWidget(), new InterlaceWidget(), null];
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                Children = { new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) } }
            };
            LogoWidget = new RectangleWidget {
                Subtexture = new Subtexture(Textures.Gui.Logo, Vector2.Zero, Vector2.One),
                Size = new Vector2(425f, 154.7f),
                Texcoord1 = new Vector2(0f, 0.04f),
                Texcoord2 = new Vector2(1f, 0.95f),
                FillColor = Colors.High,
                OutlineColor = Color.Transparent,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center
            };
            panel1.Children.Add(LogoWidget);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            //MotdWidget = new() { Size = new Vector2(float.PositiveInfinity, 70f) };
            //obj2.Children.Add(MotdWidget);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            StackPanelWidget panel2 = new() { HorizontalAlignment = WidgetAlignment.Center };
            PracticeButton = new BevelledButtonWidget { Text = LanguageControl.Get(fName, "1"), Size = new Vector2(300f, 60f) };
            panel2.Children.Add(PracticeButton);
            panel2.Children.Add(new CanvasWidget { Size = new Vector2(8f, 0f) });
            PlayButton = new BevelledButtonWidget { Text = LanguageControl.Get(fName, "2"), Size = new Vector2(300f, 60f) };
            panel2.Children.Add(PlayButton);
            panel1.Children.Add(panel2);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 5f) });
            StackPanelWidget panel3 = new() { HorizontalAlignment = WidgetAlignment.Center };
            SettingsButton = new BevelledButtonWidget { Text = LanguageControl.Get(fName, "3"), Size = new Vector2(300f, 60f) };
            panel3.Children.Add(SettingsButton);
            panel3.Children.Add(new CanvasWidget { Size = new Vector2(8f, 0f) });
            MoreGamesButton = new BevelledButtonWidget { Text = LanguageControl.Get(fName, "4"), Size = new Vector2(300f, 60f) };
            panel3.Children.Add(MoreGamesButton);
            panel1.Children.Add(panel3);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            panel1.Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Center,
                    Children = {
                        new StackPanelWidget {
                            HorizontalAlignment = WidgetAlignment.Center,
                            Children = {
                                new LabelWidget { Text = LanguageControl.Get(fName, "5"), FontScale = 0.75f },
                                new LinkWidget {
                                    Text = LanguageControl.Get(fName, "6"),
                                    Url = "https://kaalus.wordpress.com",
                                    IsEnabled = false,
                                    Color = Colors.High,
                                    FontScale = 0.75f
                                },
                                new LabelWidget { Text = LanguageControl.Get(fName, "7") + VersionsManager.Version, FontScale = 0.75f }
                            }
                        },
                        new StackPanelWidget {
                            HorizontalAlignment = WidgetAlignment.Center,
                            Children = {
                                new LabelWidget { Text = LanguageControl.Get(fName, "9"), FontScale = 0.75f },
                                new LinkWidget {
                                    Text = "Xiaofengdizhu/RuthlessConquestForSCAPI",
                                    Url = "https://gitee.com/Xiaofengdizhu/RuthlessConquestForSCAPI/",
                                    IsEnabled = false,
                                    Color = Colors.High,
                                    FontScale = 0.75f
                                }
                            }
                        }
                    }
                }
            );
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 5f) });
            root[3] = panel1;
            Children.Add(root);
            SwitchLanguageButton = new BevelledButtonWidget { Size = new Vector2(60f, 60f) };
            SwitchLanguageButton.AddChildren(
                new RectangleWidget {
                    Size = new Vector2(28f, 28f),
                    TextureLinearFilter = true,
                    HorizontalAlignment = WidgetAlignment.Center,
                    VerticalAlignment = WidgetAlignment.Center,
                    Subtexture = ContentManager.Get<Subtexture>("Textures/Gui/Language"),
                    OutlineColor = Color.Transparent,
                    FillColor = Color.White,
                    IsVisible = true,
                    TextureAnisotropicFilter = true,
                    BlendState = BlendState.NonPremultiplied
                }
            );
            Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Near,
                    VerticalAlignment = WidgetAlignment.Far,
                    Margin = new Vector2(8f, 8f),
                    Children = { SwitchLanguageButton }
                }
            );
            SCEntryButton = new BevelledButtonWidget { Size = new Vector2(60f, 60f) };
            SCEntryButton.AddChildren(
                new RectangleWidget {
                    Size = new Vector2(32f, 32f),
                    TextureLinearFilter = true,
                    HorizontalAlignment = WidgetAlignment.Center,
                    VerticalAlignment = WidgetAlignment.Center,
                    Subtexture = ContentManager.Get<Subtexture>("RCTextures/Gui/Survivalcraft2Icon"),
                    OutlineColor = Color.Transparent,
                    FillColor = Color.White,
                    IsVisible = true,
                    TextureAnisotropicFilter = true,
                    BlendState = BlendState.NonPremultiplied
                }
            );
            Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Far,
                    VerticalAlignment = WidgetAlignment.Far,
                    Margin = new Vector2(8f, 8f),
                    Children = { SCEntryButton }
                }
            );
        }

        public override void Enter(object[] parameters) {
            //Children.Find<MotdWidget>().Restart();
        }

        public override void Leave() {
            Keyboard.BackButtonQuitsApp = false;
        }

        public override void Update() {
            if (PracticeButton.IsClicked) {
                ScreensManager.SwitchScreen("RCPracticeSetup");
            }
            if (PlayButton.IsClicked) {
                if (!SettingsManager.InstructionsShown) {
                    DialogsManager.ShowDialog(
                        null,
                        new MessageDialog(
                            LanguageControl.Get(fName, "1"),
                            LanguageControl.Get(fName, "8"),
                            LanguageControl.Yes,
                            LanguageControl.No,
                            delegate(MessageDialogButton b) {
                                if (b == MessageDialogButton.Button1) {
                                    ScreensManager.SwitchScreen("RCPracticeSetup");
                                }
                                else {
                                    ScreensManager.SwitchScreen("RCGameList");
                                }
                            }
                        )
                    );
                }
                else {
                    ScreensManager.SwitchScreen("RCGameList");
                }
            }
            if (SettingsButton.IsClicked) {
                ScreensManager.SwitchScreen("RCSettings");
            }
            if (MoreGamesButton.IsClicked) {
                ScreensManager.SwitchScreen("RCCredits");
            }
            if (SwitchLanguageButton.IsClicked) {
                IOrderedEnumerable<KeyValuePair<string, CultureInfo>> sorted = LanguageControl.LanguageTypes.OrderBy(item => {
                        if (item.Key == "en-US") {
                            return 0;
                        }
                        if (item.Key == "zh-CN") {
                            return 1;
                        }
                        return 2;
                    }
                );
                DialogsManager.ShowDialog(
                    null,
                    new ListSelectionDialog(
                        null,
                        sorted,
                        70f,
                        item => ((KeyValuePair<string, CultureInfo>)item).Value.NativeName,
                        delegate(object item) { LanguageControl.ChangeLanguage(((KeyValuePair<string, CultureInfo>)item).Key); }
                    )
                );
            }
            if (Input.Back
                || SCEntryButton.IsClicked) {
                ScreensManager.SwitchScreen("MainMenu");
            }
        }
    }
}